package atom.core0.global;
/**
 * <p>Title: </p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2011.12</p>
 * @author 赵明才
 * @version 1.0
 */

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import atom.core0.exception.BizException;
import atom.core0.exception.BizSysException;
import atom.core0.util.IPUtil;
import atom.core0.util.StringUtil;

//数据库来自spring bean global-config-bean.xml

public class GlobalCache
{
	private static String deskey="";
	private static Map cacheMap = new ConcurrentHashMap<String,ConcurrentHashMap<String,Object>>();//分类别存储
//	private static String subsystemCode="";
//	private static String subsystemName="";
	
	public static void setCache(String cacheType,String cacheKey,Object cacheValue)
	{
		Map parmMap = (Map)cacheMap.get(cacheType);
		if (parmMap==null)
		{
			parmMap = new ConcurrentHashMap<String,String>();
			cacheMap.put(cacheType, parmMap);
		}

		parmMap.put(cacheKey, cacheValue);
	}
	public static void removeCache(String cacheType,String cacheKey)
	{
		Map parmMap = (Map)cacheMap.get(cacheType);
		if (parmMap!=null)
		{
			parmMap.remove(cacheKey);
		}
		//log.info("没有此类型["+cacheType+"]的缓存");
		
	}
	public static void setCache(String cacheType,Map mapvalue)
	{
		Map parmMap = (Map)cacheMap.get(cacheType);
		if (parmMap==null)
		{
			parmMap = new ConcurrentHashMap<String,Object>();
			cacheMap.put(cacheType, parmMap);
		}
		//properties 遍历propertity
		Iterator it=mapvalue.entrySet().iterator();
		while(it.hasNext()){
		    Map.Entry entry=(Map.Entry)it.next();
		    String key = (String)entry.getKey();
		    Object value = entry.getValue();
		    parmMap.put(key, value);
		}		
		
	}
	public static String getCache(String cacheType,String cacheKey)
	{
		String cacheValue;
		Map parmMap = (Map)cacheMap.get(cacheType);
		if (parmMap==null)
			return null;
		String value = (String)parmMap.get(cacheKey);
		if (value==null)
			value = "";
		return value;
	}
	public static String getMainCache(String cacheKey)
	{
		try
		{
			return getCache(GlobalCacheConst.TYPE_SYSTEM,cacheKey);
		}
		catch (Exception e)
		{
			return "";
		}
	}	
	public static Map getCache(String cacheType)
	{
		String cacheValue;
		Map parmMap = (Map)cacheMap.get(cacheType);
		if (parmMap==null)
			return null;
		return parmMap;
	}
	//

	public static String getNodeno()
	{
		try
		{
			return getCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.NODENO);
		}
		catch (Exception e)
		{
			return "";
		}
	}
	public static void setNodeno(String nodeno)
	{
		//+IPUtil.getLocalIPV4()
		GlobalCache.setCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.NODENO,nodeno);
	}	


	public static void setGlobalErrorUrl(String url)
	{
		GlobalCache.setCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.GLOBAL_ERROR_URL, url);
	}
	public static String getGlobalErrorUrl()
	{
		try
		{
			return getCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.GLOBAL_ERROR_URL);
		}
		catch (Exception e)
		{
			return "";
		}
	}
	public static String getGlobalOperErrorUrl()
	{
		try
		{
			return getCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.GLOBAL_OPER_ERROR_URL);
		}
		catch (Exception e)
		{
			return "";
		}
	}
	public static String getSystemCache(String key)
	{
		try
		{
			return getCache(GlobalCacheConst.TYPE_SYSTEM,key);
		}
		catch (Exception e)
		{
			return "";
		}
	}

	public static Map getCacheMap() {
		return cacheMap;
	}
	public static void setCacheMap(Map cacheMap) {
		GlobalCache.cacheMap = cacheMap;
	}
	public static String getGroupno()
	{
		try
		{
			return getCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.GROUPNO);
		}
		catch (Exception e)
		{
			return "";
		}
	}
	public static void setGroupno(String groupno)
	{
		GlobalCache.setCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.GROUPNO, groupno);
	}

	public static String getLocalIP() throws BizSysException
	{
		String ip = GlobalCache.getCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.KEY_IP);
		if (StringUtil.isEmpty(ip))
		{
			ip = IPUtil.getLocalIPV4();
			GlobalCache.setCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.KEY_IP,ip);
		}
		return ip;
	}
	// 定义了一些常用的全局信息的函数
	public static void setSubsystemCode(String subsystem_code)
	{
		BizException.initSubsystemCode(subsystem_code);
		GlobalCache.setCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.SUBSYSTEM_CODE,subsystem_code);
	}


	public static String getSubsystemCode()
	{
		return GlobalCache.getCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.SUBSYSTEM_CODE);

	}



	public static String getSubsystemName() {
		return GlobalCache.getCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.SUBSYSTEM_NAME);
	}


	public static void setSubsystemName(String subsystemName) {
		GlobalCache.setCache(GlobalCacheConst.TYPE_SYSTEM,GlobalCacheConst.SUBSYSTEM_NAME,subsystemName);
	}
	public static String getDeskey() {
		return deskey;
	}
	public static void setDeskey(String deskey) {
		GlobalCache.deskey = deskey;
	}


	
}
